package com.wmh.baseservice.robot.controller;

import cn.hutool.core.util.StrUtil;
import com.wmh.baseservice.common.mybatisplus.structure.LambdaQueryWrapperX;
import com.wmh.baseservice.common.utils.page.pojo.PageResult;
import com.wmh.baseservice.common.utils.page.util.PageUtil;
import com.wmh.baseservice.common.utils.returnbody.ReturnBody;
import com.wmh.baseservice.robot.entity.RobotTask;
import com.wmh.baseservice.robot.pojo.vo.RobotTaskPageReqVO;
import com.wmh.baseservice.robot.pojo.vo.RobotTaskUpdateVO;
import com.wmh.baseservice.robot.service.RobotTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

import java.util.List;

import static com.wmh.baseservice.common.utils.returnbody.ReturnBody.success;

/**
 * @author dongaolong
 * @createDate 2023-07-06
 */
@Validated
@RestController
@RequestMapping("robot/task")
@Api(tags = "自动化机器端任务管理")
@Slf4j
public class RobotTaskController {

    @Resource
    private RobotTaskService robotTaskService;

    @ApiOperation("获取分页列表")
    @GetMapping("/page")
    public String page(RobotTaskPageReqVO reqVO) {
        PageResult<RobotTask> pageResult = new PageUtil<RobotTask>(reqVO).startPage((page, wrapper) -> {
            wrapper.like(StrUtil.isNotBlank(reqVO.getName()), RobotTask.COL_NAME, reqVO.getName());
            robotTaskService.page(page, wrapper);
        });
        log.info("pageResult {}", pageResult);
        return success(pageResult);
    }

    @ApiOperation("添加任务")
    @PostMapping("/commit")
    public String commit(@NotBlank(message = "请提交名称") String name,
                         @NotBlank(message = "请提交路由") String viewRouter) {
        robotTaskService.commitRobotTask(name, viewRouter);
        return ReturnBody.success();
    }

    @ApiOperation("删除任务")
    @PostMapping("/delete")
    public String deleteRobotTaskById(@NotNull(message = "请上传主键") Long id) {
        robotTaskService.deleteRobotTaskById(id);
        return ReturnBody.success();
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    public String updateById(@Valid RobotTaskUpdateVO reqVO) {
        robotTaskService.updateRobotTaskById(reqVO);
        return ReturnBody.success();
    }

    @ApiOperation("修改状态")
    @PostMapping("/status")
    public String modifyStatusById(@NotNull(message = "请上传主键") Long id) {
        robotTaskService.modifyStatusById(id);
        return ReturnBody.success();
    }

    @GetMapping("/list")
    public String list() {
        List<RobotTask> list = robotTaskService.list(new LambdaQueryWrapperX<RobotTask>()
                .eq(RobotTask::getEnable, 1)
                .orderByDesc(RobotTask::getCreateTime));
        return ReturnBody.success(list);
    }
}
